#pragma once

#include "base.h"
#include <stdlib.h>

namespace cgen {
namespace internal {

// ----------------------------------------------------------------------------
// General helper functions

// Returns true iff x is a power of 2.  Does not work for zero.
template <typename T>
static inline bool IsPowerOf2(T x) {
  return (x & (x - 1)) == 0;
}


// The C++ standard leaves the semantics of '>>' undefined for
// negative signed operands. Most implementations do the right thing,
// though.
static inline int ArithmeticShiftRight(int x, int s) {
  return x >> s;
}


// Compute the 0-relative offset of some absolute value x of type T.
// This allows conversion of Addresses and integral types into
// 0-relative int offsets.
template <typename T>
static inline intptr_t OffsetFrom(T x) {
  return x - static_cast<T>(0);
}


// Compute the absolute value of type T for some 0-relative offset x.
// This allows conversion of 0-relative int offsets into Addresses and
// integral types.
template <typename T>
static inline T AddressFrom(intptr_t x) {
  return static_cast<T>(0) + x;
}


// Return the largest multiple of m which is <= x.
template <typename T>
static inline T RoundDown(T x, int m) {
  ASSERT(IsPowerOf2(m));
  return AddressFrom<T>(OffsetFrom(x) & -m);
}


// Return the smallest multiple of m which is >= x.
template <typename T>
static inline T RoundUp(T x, int m) {
  return RoundDown(x + m - 1, m);
}


template <typename T>
static int Compare(const T& a, const T& b) {
  if (a == b)
    return 0;
  else if (a < b)
    return -1;
  else
    return 1;
}


template <typename T>
static int PointerValueCompare(const T* a, const T* b) {
  return Compare<T>(*a, *b);
}


// Returns the smallest power of two which is >= x. If you pass in a
// number that is already a power of two, it is returned as is.
uint32_t RoundUpToPowerOf2(uint32_t x);


template <typename T>
static inline bool IsAligned(T value, T alignment) {
  ASSERT(IsPowerOf2(alignment));
  return (value & (alignment - 1)) == 0;
}


// Returns true if (addr + offset) is aligned.
static inline bool IsAddressAligned(Address addr, int alignment, int offset) {
  int offs = OffsetFrom(addr + offset);
  return IsAligned(offs, alignment);
}


// Returns the maximum of the two parameters.
template <typename T>
static T Max(T a, T b) {
  return a < b ? b : a;
}


// Returns the minimum of the two parameters.
template <typename T>
static T Min(T a, T b) {
  return a < b ? a : b;
}


// ----------------------------------------------------------------------------
// BitField is a help template for encoding and decode bitfield with
// unsigned content.
template<class T, int shift, int size>
class BitField {
 public:
  // Tells whether the provided value fits into the bit field.
  static bool is_valid(T value) {
    return (static_cast<uint32_t>(value) & ~((1U << (size)) - 1)) == 0;
  }

  // Returns a uint32_t mask of bit field.
  static uint32_t mask() {
    return (1U << (size + shift)) - (1U << shift);
  }

  // Returns a uint32_t with the bit field value encoded.
  static uint32_t encode(T value) {
    ASSERT(is_valid(value));
    return static_cast<uint32_t>(value) << shift;
  }

  // Extracts the bit field from the value.
  static T decode(uint32_t value) {
    return static_cast<T>((value >> shift) & ((1U << (size)) - 1));
  }
};


// ----------------------------------------------------------------------------
// Support for compressed, machine-independent encoding
// and decoding of integer values of arbitrary size.

// Encoding and decoding from/to a buffer at position p;
// the result is the position after the encoded integer.
// Small signed integers in the range -64 <= x && x < 64
// are encoded in 1 byte; larger values are encoded in 2
// or more bytes. At most sizeof(int) + 1 bytes are used
// in the worst case.
byte* EncodeInt(byte* p, int x);
byte* DecodeInt(byte* p, int* x);


// Encoding and decoding from/to a buffer at position p - 1
// moving backward; the result is the position of the last
// byte written. These routines are useful to read/write
// into a buffer starting at the end of the buffer.
byte* EncodeUnsignedIntBackward(byte* p, unsigned int x);

// The decoding function is inlined since its performance is
// important to mark-sweep garbage collection.
inline byte* DecodeUnsignedIntBackward(byte* p, unsigned int* x) {
  byte b = *--p;
  if (b >= 128) {
    *x = static_cast<unsigned int>(b) - 128;
    return p;
  }
  unsigned int r = static_cast<unsigned int>(b);
  unsigned int s = 7;
  b = *--p;
  while (b < 128) {
    r |= static_cast<unsigned int>(b) << s;
    s += 7;
    b = *--p;
  }
  // b >= 128
  *x = r | ((static_cast<unsigned int>(b) - 128) << s);
  return p;
}

}} // namespace cgen::internal

